package com.harmony.pilot.knowledge.controller;

import com.harmony.pilot.knowledge.service.ScanService;
import com.harmony.pilot.knowledge.service.ScanService.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.CompletableFuture;

/**
 * 扫描控制器
 * 管理代码扫描任务
 */
@Slf4j
@RestController
@RequestMapping("/api/knowledge/scan")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class ScanController {
    
    private final ScanService scanService;
    
    /**
     * 开始全量扫描
     * 
     * POST /api/knowledge/scan/start
     */
    @PostMapping("/start")
    public CompletableFuture<ScanResult> startFullScan() {
        log.info("收到全量扫描请求");
        return scanService.startFullScan();
    }
    
    /**
     * 开始增量扫描
     * 
     * POST /api/knowledge/scan/incremental
     * 
     * Request (可选):
     * {
     *   "lastCommitHash": "abc123..."  // 上次扫描的commit hash
     * }
     */
    @PostMapping("/incremental")
    public CompletableFuture<ScanResult> startIncrementalScan(
            @RequestBody(required = false) IncrementalScanRequest request) {
        log.info("收到增量扫描请求，lastCommitHash: {}", 
                request != null ? request.getLastCommitHash() : "null");
        
        String lastCommitHash = request != null ? request.getLastCommitHash() : null;
        return scanService.startIncrementalScan(lastCommitHash);
    }
    
    /**
     * 增量扫描请求DTO
     */
    public static class IncrementalScanRequest {
        private String lastCommitHash;
        
        public String getLastCommitHash() { return lastCommitHash; }
        public void setLastCommitHash(String lastCommitHash) { this.lastCommitHash = lastCommitHash; }
    }
    
    /**
     * 获取扫描状态
     * 
     * GET /api/knowledge/scan/status
     */
    @GetMapping("/status")
    public ScanStatusResponse getScanStatus() {
        ScanStatus status = scanService.getScanStatus();
        boolean isScanning = scanService.isScanning();
        
        ScanStatusResponse response = new ScanStatusResponse();
        response.setScanning(isScanning);
        response.setTotalFiles(status.getTotalFiles());
        response.setProcessedFiles(status.getProcessedFiles());
        response.setSuccessCount(status.getSuccessCount());
        response.setFailCount(status.getFailCount());
        response.setProgress(status.getProgress());
        response.setElapsedTime(status.getElapsedTime());
        
        return response;
    }
    
    // DTO
    public static class ScanStatusResponse {
        private boolean scanning;
        private int totalFiles;
        private int processedFiles;
        private int successCount;
        private int failCount;
        private double progress;
        private long elapsedTime;
        
        // getters and setters
        public boolean isScanning() { return scanning; }
        public void setScanning(boolean scanning) { this.scanning = scanning; }
        public int getTotalFiles() { return totalFiles; }
        public void setTotalFiles(int totalFiles) { this.totalFiles = totalFiles; }
        public int getProcessedFiles() { return processedFiles; }
        public void setProcessedFiles(int processedFiles) { this.processedFiles = processedFiles; }
        public int getSuccessCount() { return successCount; }
        public void setSuccessCount(int successCount) { this.successCount = successCount; }
        public int getFailCount() { return failCount; }
        public void setFailCount(int failCount) { this.failCount = failCount; }
        public double getProgress() { return progress; }
        public void setProgress(double progress) { this.progress = progress; }
        public long getElapsedTime() { return elapsedTime; }
        public void setElapsedTime(long elapsedTime) { this.elapsedTime = elapsedTime; }
    }
}

